<template>
  <div>
    <el-table
      row-key="id"
      default-expand-all
      :tree-props="{ children: 'children' }"
      :data="store.getters['menu/getMenu']"
      border
      style="width: 100%"
    >
      <el-table-column prop="id" label="菜单编号" width="80" />
      <el-table-column prop="title" label="菜单名称" width="180" />
      <el-table-column prop="pid" label="上级菜单" />
      <el-table-column label="菜单图标">
        <template v-slot="item">
          <div>
            <el-icon><component :is="item.row.icon"></component></el-icon>
            <!-- {{item.row.icon}} -->
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="菜单地址" />
      <el-table-column label="状态">
        <template v-slot="item">
          <div>
            {{ item.row.status == 1 ? "启用" : "禁用" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="item">
          <div>
            <el-button type="primary" @click="edit(item.row.id)">编辑</el-button>
            <el-button type="danger" @click="del(item.row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { menuDelete } from "../../request/api";
import {defineEmits} from 'vue'
//引入仓库方法
import { useStore } from "vuex";
import { ElMessageBox, ElMessage } from "element-plus";
let emit =   defineEmits(['edit'])
//定义个仓库
let store = useStore();
console.log(store, 99999);
//组件一加载触发行动
store.dispatch("menu/changeMenuAction");
//封装一个删除事件
let del = (id:number) => {
  ElMessageBox.confirm("你确定要删除这条数据吗?", "Warning", {
    confirmButtonText: "OK",
    cancelButtonText: "Cancel",
    type: "warning",
  })
    .then(() => {
      menuDelete({
        id,
      }).then((res:any) => {
        if(res.data.code==200){
          store.dispatch("menu/changeMenuAction");
          ElMessage.success(res.data.msg)
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "Delete canceled",
      });
    });
};
//封装一个编辑事件
let edit = (id:number)=>{
  emit('edit',id)
}
</script>

<style scoped></style>
